//
//  WelcomeViewController.swift
//  新浪微博
//
//  Created by yzl on 15/8/2.
//  Copyright © 2015年 yjc. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomeViewController: UIViewController {
/// 定义一个底部图像约束
    private var iconBottomConstraint: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        // 加载用户头像
        if let urlString = userAccount.shareAccount?.avatar_large {
            // 更新图像
            userIconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 修改底部约束
        iconBottomConstraint?.constant = -UIScreen.mainScreen().bounds.height - (iconBottomConstraint?.constant)!
        // 动画效果
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 强制更新约苏
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
              // 发送通知
            NSNotificationCenter.defaultCenter().postNotificationName(JCRootViewControllerSwitchNotification, object: true)
        }
    }
/// 准备界面
    private func prepareUI() {
        view.addSubview(backImageView)
        view.addSubview(userIconView)
        view.addSubview(label)
 /// 自动布局
        // 1,背景图片
        backImageView.ff_Fill(view )
//        backImageView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|",options:NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:[ "subview":backImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views:[ "subview":backImageView]))
        
 /// 头像,返回一个素组
   let cons = userIconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(90, 90), offset: CGPoint(x: 0, y: -160))
//        userIconView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: userIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item:userIconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//         view.addConstraint(NSLayoutConstraint(item: userIconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//         view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: userIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        /// 始终获取最后一个约束，记录底边约束
        iconBottomConstraint = userIconView.ff_Constraint(cons, attribute:NSLayoutAttribute.Bottom )
        //iconBottomConstraint = view.constraints.last
 /// 标签
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: userIconView, size: nil, offset: CGPoint(x: 0, y: 16))
//        label.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: userIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: userIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
    }
// MARK: -懒加载控件
/// 背景图片
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
/// 用户头像
   private lazy var userIconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named:  "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
/// 消息文字
    private lazy var label: UILabel = {
        let lab = UILabel()
        lab.text = "欢迎归来"
        lab.sizeToFit()
        return lab
    }()
}
